<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<%@ include file="/common/import.jsp" %>
<link href="${ctx}/css/hall.css" rel="stylesheet">
<body>
<%@ include file="/common/halltop.jsp" %>
<div class="wrap" style="height:500px;">
<div style="margin:10px;width:28%;height:300px;float:left;background:#ffaaff">
标的图【${loginUser.userEname}】
</div>
<div style="padding:10px;width:40%;float:left;">
	<table class="info" cellspacing="1" cellpadding="0">
		<tr>
			<th>标的名称：</th>
			<td colspan=3>${item.itemName }</td>
		</tr>
		<tr>
			<th>起始价格：</th>
			<td>${item.firstValuePre }</td>
			<th>加价幅度：</th>
			<td><span id="span_increment">${item.incrementValue }</span></td>
		</tr>
		<tr>
			<th>最新价格：</th>
			<td><font  id='last_value'>${price.lastValue }</font> (${item.priceUnit})${item.currency}</td>
			<th>最新报价人：</th>
			<td id="user_name">${price.userName }</td>
		</tr>
		<tr>
			<th>报价次数：</th>
			<td id="bid_num">${price.bidTimes }</td>
			<th>最新报价时间：</th>
			<td id="last_bid_time">${price.lastBidTime }</td>
		</tr>		
		<tr>
			<th>所在期：</th>
			<td><font id="itemStatus"></font></td>
			<th>剩余时间：</th>
			<td><font id="spareTime"></font></td>
		</tr>
	</table>	
	<center id="baojiaBtn" style="margin:10px;">
		<div id="status_BC" style="display:none;">
		  <c:if test="${'1'==item.bidMode }">
			<div id="div_btn_list" style="display:none;"></div>
		<!--  <a class="easyui-linkbutton" iconCls="icon-add" onclick="baojia(1);"><font id="b1"></font></a>
				<a class="easyui-linkbutton" iconCls="icon-add" onclick="baojia(2);"><font id="b2"></font></a>
				<a class="easyui-linkbutton" iconCls="icon-add" onclick="baojia(3);"><font id="b3"></font></a>
				<a class="easyui-linkbutton" iconCls="icon-add" onclick="baojia(4);"><font id="b4"></font></a>
				<a class="easyui-linkbutton" iconCls="icon-add" onclick="baojia(5);"><font id="b5"></font></a>
				-->
				<br/>
				
				<!-- 
				1）允许报起始价+无报价情况
				2）开启V型竞价+无报价情况
				 -->
				<c:if test="${(item.allowFirstValue=='T' && price.bidTimes==0) || (item.vFlag=='T' && price.bidTimes==0) }">
					<a id="btnAllowFirst" class="easyui-linkbutton" iconCls="icon-add" onclick="baojia(0);">起始价</a>
				</c:if>
				<c:if test="${item.ifRight=='T'}">
					<a id="btnifRight" class="easyui-linkbutton" style="display:none;" iconCls="icon-add" onclick="baojia(0);">我要行权</a>
				</c:if>
				<c:if test="${!empty item.oncevalue && item.oncevalue!='0'}">
					<a id="btnoncevalue" class="easyui-linkbutton"  iconCls="icon-add" onclick="baojia('A');">一口价￥${item.oncevalue}</a>
				</c:if>
			</c:if>
			 <c:if test="${'3'==item.bidMode }">
			 	<div id="div_btn_list" ></div>
			 	<a id="btnAllowFirst" class="easyui-linkbutton" iconCls="icon-add" onclick="baojia(0);">起始价</a>
			 </c:if>
			 	
				
		</div>
		<div id="status_D" style="display:none;">
				<center id="status_D_1" style="margin:10px;">
							加价：<input id="myPrice" name="myPrice" /> 
							<a id="btnAddPrice" class="easyui-linkbutton"  iconCls="icon-add" onclick="baojiaAdd();">加价</a>
				</center>
				<center id="status_D_2" style="margin:10px;">最高报价人正在加价过程。。。</center>
		</div>
		<div id="status_E" style="display:none;">
				<center id="status_E_1" style="margin:10px;">
							<a id="btnAddPrice" class="easyui-linkbutton"  iconCls="icon-add" onclick="xingquan('T');">我要行权</a> | 
							<a id="btnAddPrice" class="easyui-linkbutton"  iconCls="icon-add" onclick="xingquan('F');">放弃行权</a>
				</center>
				<center id="status_E_2" style="margin:10px;">行权人正在行权过程。。。</center>
		</div>
		<div id="status_9" style="display:none;">
				<div id="status_9_1">恭喜你！您已是最高报价人</div>
				<div id="status_9_2">报价已结束！</div>
		</div>
	</center>
	
	</div>
	<div style="padding:2px;width:25%;height:300px;float:right;">
	<table id="info_table" class="info" cellspacing="1" cellpadding="0">
		<tr>
			<th>报价人</th>
			<th>报价价格</th>	
			<th>报价时间</th>
		</tr>
	</table>
	<script type="text/template" id="info_html">
	<? for ( var i = 0; i < rows.length; i++) {?>
		<tr>
			<td><?=rows[i].userName?></td>
			<td><?=rows[i].bidValue?></td>
			<td><?=rows[i].bidTime?></td>
		</tr>
	<?}?>  		
	</script>
	</div>
</div>
<%@ include file="/common/hallbottom.jsp" %>
</body>
</html>
<script>
var buttonNum = 5;
var myLevel = "${myLevel}";//当前登录人针对此标的的行权级别
var itemId= "${item.itemId}";
var decimalCount = "${item.decimalCount}";
var currentBidTime="0";
///如果lastValue为空取firstValue;
var lastValue = "${price.lastValue}";//${price.lastValue}==0? ${price.firstValue} : ${price.lastValue};
if(lastValue=="0"|| lastValue=="")lastValue = "${price.firstValue}";
var incrementGroup ="${item.incrementGroup}";
var incrementValue ="${item.incrementValue}";
var increment=getIncrement();
var loginUser = "${loginUser.userEname}";//当前登录人
var lastUser = "${price.userName}";//最高报价人
var vFlag = "${item.vFlag}";
//获取加价幅度控制
function getIncrement(){
	var increment ;
	if(incrementValue.indexOf('|')==-1){
		increment = incrementValue;
		return increment;
	}else{
		var incrementGroupArray= incrementGroup.split("|");;//1000|2000
		var incrementArray = incrementValue.split("|");//100|200
		for(var i =incrementGroupArray.length;i>0;i--){
			//alert(lastValue+">="+incrementGroupArray[i]+"="+lastValue>=incrementGroupArray[i])
			if(lastValue>=incrementGroupArray[i]){
				increment = incrementArray[i];
				return increment;
			}
		}
	}
}

function bidGet(){
	var bidGetUrl = ctx+spaceDomain+'hall/bidGet';
	var bidNum=$("#bid_num").html();
	$.ajax({
		   type:'get',
		   url:bidGetUrl,
		   data:{itemId:itemId,bidNum:currentBidTime},
		   dataType:'json',
		   async:false,
		   success: function(myData){
			    $("#spareTime").html(spareTxt(myData.spareTime));
			    //alert(myData.ischange);
			   if(myData.ischange){
				   //incrementValue = getNewincrementValue(myData.price.lastValue,{item.increntGroup},{item.increntValue});
				   $("#last_value").html(formatNumber(myData.price.lastValue,decimalCount,0));
				   lastValue = myData.price.lastValue;
				   increment =getIncrement();
				   $("#span_increment").text(increment);
				   lastUser = myData.price.userName;
				   setButton();
				   $("#user_name").html(myData.price.userName);
				   $("#bid_num").html(myData.price.bidTimes);
				   currentBidTime = myData.price.bidTimes;
				   $("#last_bid_time").html(myData.price.lastBidTime);
				   
				   if("1"=="${item.bidMode}"){//非一次报价
					   if(myData.price.bidTimes==0){
						   $("#btnAllowFirst").show();
						   $("#div_btn_list").hide();
					   }else{
						   $("#btnAllowFirst").hide();
						   $("#div_btn_list").show();
					   }
				   }				   
				   
				   //加载报价记录
				   setBaoJiaInfo(myData);
				   
				   //行权控制，上一个最优报价人的行权级别
				   //alert(myLevel+"=="+myData.lastLevel);
				   if(myLevel>myData.lastLevel){
					   $("#btnifRight").show();
				   }else{
					   $("#btnifRight").hide(); 
				   }
				   
			   }
			   setStatus(myData.itemStatus);
		  }
	});	
}
//加载报价记录
function setBaoJiaInfo(myData){
	deleteAllRow("info_table");
	var myDataBaoJia = new Object();
	myDataBaoJia.rows = myData.list;
	var htmlData = baidu.template('info_html',eval(myDataBaoJia));
	if(htmlData){
		   $("#info_table").append(htmlData);
	   }
}
//控制显示区域
var itemStatusOld ="";
function setStatus(status){
	$("#itemStatus").html(itemStatusTxt(status));
	if(itemStatusOld!=status){
		//默认全部隐藏
		$("#status_BC").hide();
		$("#status_D").hide();
		$("#status_E").hide();
		$("#status_9").hide();
		
		//控制每个显示区域里的内容
		if("B"==status){
			$("#status_BC").show();
			
		}else if ("C"==status){
			$("#status_BC").show();
			
		}else if ("D"==status){
			$("#status_D").show();
			if(loginUser==lastUser){
				$("#status_D_1").show();
				$("#status_D_2").hide();
			}else{
				$("#status_D_2").show();
				$("#status_D_1").hide();
			}
		}else if ("E"==status){
			$("#status_E").show();
			if(myLevel=="9"){
				$("#status_E_1").show();
				$("#status_E_2").hide();
			}else{
				$("#status_E_1").hide();
				$("#status_E_2").show();
			}
		}else if ("9"==status || "Z"==status){
			if("9"==status){
			clearInterval(interv);
			}
			$("#status_9").show();
			$("#spareTime").html("-");
			if(loginUser==lastUser){
				$("#status_9_1").show();
				$("#status_9_2").hide();
			}else{
				$("#status_9_2").show();
				$("#status_9_1").hide();
			}
		}
	}
	itemStatusOld=status;
}

//正常报价
function baojia(n){
	//alert(n);
	var myPrice = "";
	if("A"==n){
		myPrice = formatNumber('${item.oncevalue}',decimalCount,0);
	}else{
		myPrice = parseFloat(n* increment) + parseFloat(lastValue);
		myPrice = formatNumber(myPrice,decimalCount,0);
	}
	if(confirm("你确实要报价"+myPrice+"吗?")){
		var bidSendUrl = ctx+spaceDomain+'hall/bidSend';
		$.ajax({
			   type:'get',
			   url:bidSendUrl,
			   data:{itemId:itemId,myPrice:myPrice},
			   dataType:'json',
			   success: function(myData){
					if(myData.flag=="T"){
						alert("报价成功");
						$("#btnAllowFirst").hide();
					}else{
						alert(myData.msg);
					}
			  }
		});	
	}
}
//行权
function xingquan(flag){
	if("T"==flag){
		if(confirm("你确实要行权吗?")){
			var bidSendUrl = ctx+spaceDomain+'hall/bidXingquan';
			$.ajax({
				   type:'get',
				   url:bidSendUrl,
				   data:{itemId:itemId,flag:flag},
				   dataType:'json',
				   success: function(myData){
						if(myData.flag=="T"){
							alert("行权成功");
							$("#btnAllowFirst").hide();
						}else{
							alert(myData.msg);
						}
				  }
			});	
		}
	}else{
		
	}
}
//加价
function baojiaAdd(){
	//alert(n);
	var myPrice = $("#myPrice").val();
	if(myPrice==undefined || myPrice==""){
		alert("请输入价格");
		return;
	}
	
	if(confirm("你确实要加价到："+myPrice+"吗?")){
		var bidSendUrl = ctx+spaceDomain+'hall/bidAdd';
		$.ajax({
			   type:'get',
			   url:bidSendUrl,
			   data:{itemId:itemId,myPrice:myPrice},
			   dataType:'json',
			   success: function(myData){
					if(myData.flag=="T"){
						alert("加价成功");
						$("#baojiaBtn").hide();
					}else{
						alert(myData.msg);
					}
			  }
		});	
	}
}
//设置报价按钮
function setButton(){
	var btnHtml = "";
	for(var i=1;i<=buttonNum;i++){
		var btnText = parseFloat(increment*i);
		btnHtml+="<a class='easyui-linkbutton' iconCls='icon-add' onclick=\"baojia("+i+");\"> + "+btnText+"</a>";
	}
	$("#div_btn_list").html(btnHtml);
}

var interv;
$(function(){
	setButton();
	interv = setInterval(bidGet,1000);
});

/**
* 将数值四舍五入后格式化.
*
* @param num 数值(Number或者String)
* @param cent 要保留的小数位(Number)
* @param isThousand 是否需要千分位 0:不需要,1:需要(数值类型);
* @return 格式的字符串,如'1,234,567.45'
* @type String
*/
function formatNumber(num,cent,isThousand) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))//检查传入数值为数值类型.
     num = "0";
if(isNaN(cent))//确保传入小数位为数值型数值.
cent = 0;
cent = parseInt(cent);
cent = Math.abs(cent);//求出小数位数,确保为正整数.
if(isNaN(isThousand))//确保传入是否需要千分位为数值类型.
isThousand = 0;
isThousand = parseInt(isThousand);
if(isThousand < 0)
isThousand = 0;
if(isThousand >=1) //确保传入的数值只为0或1
isThousand = 1;
    sign = (num == (num = Math.abs(num)));//获取符号(正/负数)
//Math.floor:返回小于等于其数值参数的最大整数
    num = Math.floor(num*Math.pow(10,cent)+0.50000000001);//把指定的小数位先转换成整数.多余的小数位四舍五入.
    cents = num%Math.pow(10,cent); //求出小数位数值.
    num = Math.floor(num/Math.pow(10,cent)).toString();//求出整数位数值.
cents = cents.toString();//把小数位转换成字符串,以便求小数位长度.
while(cents.length<cent){//补足小数位到指定的位数.
    cents = "0" + cents;
}

//ymyang 如果 小数位不为0，则显示小数位


if(cent!="0"){
	cents = '.' + cents ;
}else{
	cents = "";
}

if(isThousand == 0) //不需要千分位符.
    return (((sign)?'':'-') + num + cents);
//对整数部分进行千分位格式化.
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + num + cents);
}
</script>